/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ht.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.ht.entity.HtClient;
import com.thinkgem.jeesite.modules.ht.service.HtClientService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.ht.entity.HtRent;
import com.thinkgem.jeesite.modules.ht.service.HtRentService;

import java.util.List;

/**
 * 租赁信息Controller
 * @author yy
 * @version 2016-12-05
 */
@Controller
@RequestMapping(value = "${adminPath}/ht/htRent")
public class HtRentController extends BaseController {

	@Autowired
	private HtRentService htRentService;
	@Autowired
	private HtClientService htClientService;
	
	@ModelAttribute
	public HtRent get(@RequestParam(required=false) String id) {
		HtRent entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = htRentService.get(id);
		}
		if (entity == null){
			entity = new HtRent();
		}
		return entity;
	}
	
	@RequiresPermissions("ht:htRent:view")
	@RequestMapping(value = {"list", ""})
	public String list(HtRent htRent, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HtRent> page = htRentService.findPage(new Page<HtRent>(request, response), htRent); 
		model.addAttribute("page", page);
		return "modules/ht/htRentList";
	}

	@RequiresPermissions("ht:htRent:view")
	@RequestMapping(value = "form")
	public String form(HtRent htRent, Model model) {
		model.addAttribute("htRent", htRent);
		List<HtClient> clientList =  htClientService.findList(new HtClient());
		System.out.println("clientList size :"+clientList.size());
		model.addAttribute("clientList" ,clientList );
		return "modules/ht/htRentForm";
	}

	@RequiresPermissions("ht:htRent:edit")
	@RequestMapping(value = "save")
	public String save(HtRent htRent, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, htRent)){
			return form(htRent, model);
		}
		htRentService.save(htRent);
		addMessage(redirectAttributes, "保存租赁信息成功");
		return "redirect:"+Global.getAdminPath()+"/ht/htRent/?repage";
	}
	
	@RequiresPermissions("ht:htRent:edit")
	@RequestMapping(value = "delete")
	public String delete(HtRent htRent, RedirectAttributes redirectAttributes) {
		htRentService.delete(htRent);
		addMessage(redirectAttributes, "删除租赁信息成功");
		return "redirect:"+Global.getAdminPath()+"/ht/htRent/?repage";
	}

}